package leetcode.editor.day;

// 1417. 重新格式化字符串
// https://leetcode.cn/problems/reformat-the-string/
class ReformatTheString {
    public static void main(String[] args) {
        Solution solution = new ReformatTheString().new Solution();
    }

    //leetcode submit region begin(Prohibit modification and deletion)
    class Solution {
        public String reformat(String s) {
            int n = s.length();
            char[] res = new char[n];

            // 求出数量
            int count1 = 0, count2 = 0;
            for (int i = 0; i < n; i++) {
                char c = s.charAt(i);
                if (c <= '9' && c >= '0') count1++;
                else count2++;
            }

            // 如果差值 >1 不满足
            if (Math.abs(count1 - count2) > 1) return "";

            // 谁少谁就从第二个位置开始放
            int i = 0, j = 0;
            if (count1 > count2) j++;
            else i++;

            // 开始放入
            for (int k = 0; k < n; k++) {
                char c = s.charAt(k);
                if (c <= '9' && c >= '0') {
                    res[i] = c;
                    i += 2;
                } else {
                    res[j] = c;
                    j += 2;
                }
            }

            return new String(res);
        }
    }
//leetcode submit region end(Prohibit modification and deletion)

}
